<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

if ($EXCMS != "work") {
	header ("Location: ../../index.php");
	exit;
}

print '<ul id="top_nav"><li class="sel"><a href="admin.php">Панель администратора</a></li><li>Запрос к БД</li></ul>';

if ($_GET['query'] == "true") {
	if ($_POST['code'] == "") site_error ("Не заполнено поле");
	elseif ($_POST['code'] != "") {		
		$code_ = stripslashes($_POST['code']);
		
		if (substr($code_, (strlen($code_)-1), 1) == ";") $code_ = substr($code_, 0, (strlen($code_)-1));
		
		$code_ = explode (";", $code_);
		
		$mes_t = 0;
		$mes_f = "";
		
		foreach ($code_ as $code) {
			if ($_POST['result'] != "true") {				
				if (@mysql_query ($code)) $mes_t++;
				else $mes_f .= "\n<li>Запрос не был выполнен!<br><font color=red>".mysql_error()."</font>";
			}
			elseif ($_POST['result'] == "true") {
					if ($query = @mysql_query ($code)) {
					$rows = mysql_num_rows ($query);
					$cols = mysql_num_fields ($query);
					$table = "";
					$table .= "\n\t<tr>";
					for ($w=0; $w<$cols; $w++) {
						$table .= "\n\t\t<td valign=top><center><b>".mysql_field_name($query, $w)."</b></td>";
					}
					$table .= "\n\t</tr>";
				
					for ($i=0; $i<$rows; $i++) {
						$table .= "\n\t<tr>";
						for ($j=0; $j<$cols; $j++) {
							$table .= "\n\t\t<td valign=top>".mysql_result ($query, $i, $j)."&nbsp;</td>";
						}
						$table .= "\n\t</tr>";
					}
					past_table ("Результат запроса", "<table class=main_text border=1>".$table."</table>");
				}
				else site_error ("Запрос не был выполнен!<br><font color=red>".mysql_error()."</font>");
			}
		}
		
		if ($mes_t != 0 && $mes_f == "") site_message ("Запрос выполнен");
		if ($mes_f != "") site_error ($mes_f);
	}
}

$option = "";

$get_tables = @mysql_query ("SHOW TABLES");
while ($tables = @mysql_fetch_array($get_tables)) {
	$title = $tables[0];
	if (substr($title, 0, strlen(DB_PEREFIX)) == DB_PEREFIX) {
		$option .= "\n\t\t\t<option value=\"$title\">$title</option>";
	}
}

if ($_POST['result'] == "true") $cbox = "checked";

$form = "<form style='margin:0px' method='post' name=record action='admin.php?do=mysql&query=true'>
<table class=main_text border=0 cellpadding=2 cellspacing=0 width=100%>
	<tr>
		<td colspan=2>MySQL - код:</td>
	</tr>
	<tr>
		<td colspan=2>
			<input type=button class=button value='SELECT' onclick='insertText_code(\"SELECT \");'>
			<input type=button class=button value='FROM' onclick='insertText_code(\"FROM \");'>
			<input type=button class=button value='DELETE' onclick='insertText_code(\"DELETE \");'>
			<input type=button class=button value='UPDATE' onclick='insertText_code(\"UPDATE \");'>
			<input type=button class=button value='ORDER BY' onclick='insertText_code(\"ORDER BY \");'>
			<input type=button class=button value='DROP' onclick='insertText_code(\"DROP \");'>
			<input type=button class=button value='DESC' onclick='insertText_code(\"DESC \");'>
			<input type=button class=button value='LIMIT' onclick='insertText_code(\"LIMIT \");'>
			<input type=button class=button value='WHERE' onclick='insertText_code(\"WHERE \");'>
		</td>
	</tr>
	<tr>
		<td colspan=\"2\" valign=top align=left>
			<div style=\"float:left;\"><textarea onclick='updatePos(this);' onselect='updatePos(this);' onchange='updatePos(this);' name=code style='width:500px' class=form rows=15>".$_POST['code']."</textarea></div>
			<div style=\"float:right;\"><select class=form size=12 onchange=\"javascript:insertText_code(this.value);\"></div>
			$option
			</select>
		</td>
	</tr>
	<tr>
		<td colspan=2>
			<span class=\"important_checkbox\"><input type=checkbox name=result value=true id=result $cbox></span> <label for=result>Вывести результат</label><p>
			<input type=button class=button value='Сбросить' onclick='document.record.code.value=\"\";'>
			<span class=\"important_button\"><input style='width:200px;' type=submit class=button value='Пошел!'></span>
		</td>
	</tr>
</table></form>
<script>document.record.code.focus();</script>
\n\n".insert_script("code");

past_table ("Запрос к базе данных", $form);

come_back ("панели администратора", "admin.php");

?>
